<template>
  <div class="p-2">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="编码" prop="matCode">
              <el-input v-model="queryParams.matCode" placeholder="请输入物料编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="分类" prop="matClass">
              <el-select v-model="queryParams.matClass" placeholder="请选择物料分类" clearable filterable>
                <el-option
                  v-for="item in classList"
                  :key="item.classCode"
                  :label="item.className"
                  :value="item.classCode"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>

    <el-card shadow="never">
      <el-table ref="selectMatTable"  v-loading="loading" :data="matList" >
        <el-table-column label="主键" align="center" prop="matId" v-if="false" />
        <el-table-column label="物料编码" align="center" prop="matCode" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="物料描述" align="center" prop="matName" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="财务编码" align="center" prop="fdCode" :show-overflow-tooltip="true" width="120" />
        <el-table-column label="图号" align="center" prop="figNum" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="物料组" align="center" prop="matGroup" />
        <el-table-column label="分类" align="center" prop="matClass" />
        <el-table-column label="基本单位" align="center" prop="unitCode" />
        <el-table-column label="毛重" align="center" prop="grossWeight" />
        <el-table-column label="安全库存" align="center" prop="safetyStock" />
        <el-table-column label="标准价" align="center" prop="standardPrice" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button size="small" type="text" icon="Select" @click="handleSelect(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="SelectMat" lang="ts">
import { listMat} from '@/api/wmscloud/mat';
import { MatVO, MatQuery } from '@/api/wmscloud/mat/types';
import { MatClassVO } from '@/api/wmscloud/matClass/types';
import { listAllClass } from '@/api/wmscloud/matClass';
import { ElTable } from 'element-plus';

const matList = ref<MatVO[]>([]);
const classList = ref<MatClassVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const selectMatTable = ref<InstanceType<typeof ElTable>>();
const queryParams = reactive<MatQuery>({
    pageNum: 1,
    pageSize: 10,
    matCode: undefined,
    matName: undefined,
    fdCode: undefined,
    figNum: undefined,
    matGroup: undefined,
    matClass: undefined,
    params: {
    }
});
/** 查询物料主数据列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMat(queryParams);
  matList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 加载所有物料分类 */
const listAllClasss = async () => {
  const res = await listAllClass();
  classList.value = res.data;
}

// 确认选择的方法
const emit = defineEmits<{
  (e: 'confirmSelect', row: MatVO): void
}>();

const handleSelect = (row: MatVO) => {
  // 清空当前表格选中项
  selectMatTable.value?.clearSelection?.();
  console.log(row)
  // 通过 emit 通知父组件
  emit('confirmSelect', row);
};

onMounted(() => {
  getList();
  listAllClasss();
});
</script>
